package cn.jdy.plugins.service;

import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.framework.db.hibernate.BaseDao;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.platform.entity.ConfValue;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.HelpInfo;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wuzhiliang
 * @title : 文章操作实现
 */
@Service
public class HelpService extends BaseDao<HelpInfo> {

	@Autowired
	private ConfService confService;

	/**
	 * 按条件查询
	 * @param helpInfo
	 * @return
	 */
	@Cacheable(value = "cache1", key="'help_id' + #helpInfo.id")
	public List<HelpInfo> query(HelpInfo helpInfo) {
		if(helpInfo == null) return super.findAll();
		List<HelpInfo> list = null;
		
		if(helpInfo.getiDisplayLength() > 0){
		  list = super.find(createFilter(helpInfo),helpInfo.getiDisplayStart(),helpInfo.getiDisplayLength());
		}else{
		  list = super.find(createFilter(helpInfo));
		}

		
		if(list!=null && list.size()>0) {
			//获取栏目名称
			List<ConfValue> plist = confService.queryByType(GlobalConstant.TYPE_HELP, 0L);// 文章分类： 10  放常量;
			Map<String, String> map = new HashMap<String, String>();
			for(ConfValue bo : plist)
				map.put(String.valueOf(bo.getId()), bo.getValue0());

			//设置栏目名称
			for(int i=0,size=list.size(); i<size; i++)
				list.get(i).setColumnname(map.get(list.get(i).getColumnid()));
		}

		return list;
	}

	@Cacheable(value = "cache1", key="'help_count' + #helpInfo.id")
	public Long count(HelpInfo helpInfo) {
		return super.count(createFilter(helpInfo));
	}

	/**
	 * 拼装条件
	 * @param helpInfo
	 * @param page
	 * @return
	 */
	private DetachedCriteria createFilter(HelpInfo helpInfo) {
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		if(!StringUtil.isNullOrEmpty(helpInfo.getColumnid())){
		  dc.add(Restrictions.eq("columnid", helpInfo.getColumnid()));
		}
		if(!StringUtil.isNullOrEmpty(helpInfo.getStatus())){
		  dc.add(Restrictions.eq("status", helpInfo.getStatus()));
		}
		dc.addOrder(Order.desc("createTime"));
		return dc;
	}

	/**
	 * 按照类型查询 文章
	 * @return
	 */
	@Cacheable(value = "cache1", key="'help_type' + #columnid")
	public List<HelpInfo> findbytype(HelpInfo helpInfo, boolean page) {
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
    dc.add(Restrictions.eq("columnid", helpInfo.getColumnid()));
    dc.add(Restrictions.eq("status", "1"));
    dc.addOrder(Order.desc("createTime"));
    if(page){
      return super.find(dc,helpInfo.getiDisplayStart(),helpInfo.getiDisplayLength());
    }
    return super.find(dc);
	}
	
	/**
	 * 按帮助类型查询
	 * @param helpInfo
	 * @return
	 */
	@Cacheable(value = "cache1", key="'help_type' + #columnid")
	public List<HelpInfo> query(HelpInfo helpInfo, String typeid) {
		DetachedCriteria dc = DetachedCriteria.forClass(entityClass);
		dc.add(Restrictions.eq("columnid",typeid));
		dc.add(Restrictions.eq("status", "1"));
	    dc.addOrder(Order.desc("createTime"));
	    return super.find(dc);
	}

}
